package com.mnsk.generator.controller;

import com.mnsk.generator.common.utils.text.Convert;
import com.mnsk.generator.domain.SysDictType;
import com.mnsk.generator.service.SysDictTypeService;
import com.mnsk.generator.common.constant.Constants;
import com.mnsk.generator.common.core.controller.BaseController;
import com.mnsk.generator.common.core.domain.AjaxResult;
import com.mnsk.generator.common.core.domain.Ztree;
import com.mnsk.generator.common.core.page.TableDataInfo;
import com.mnsk.generator.query.DictTypeQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 字典类型信息
 *
 * @author admin
 */
@Controller
@RequestMapping("/system/dict")
//字典类型信息
public class DictTypeController extends BaseController {

    private static final String PREFIX = "system/dict/type";

    @Resource
    private SysDictTypeService dictTypeService;

    @GetMapping()
    public String dictType() {
        return PREFIX + "/type";
    }

    /**
     * 分页条件查询获取字典类型列表
     *
     * @param typeQuery 查询条件
     * @return 分页数据
     */
    @ResponseBody
    @PostMapping("/list")
    public TableDataInfo<SysDictType> list(DictTypeQuery typeQuery) {
        return toPageResult(dictTypeService.selectDictTypeList(typeQuery));
    }

    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add() {
        return PREFIX + "/add";
    }

    /**
     * 新增保存字典类型
     */
    @ResponseBody
    @PostMapping("/add")
    public AjaxResult<Object> addSave(@Validated SysDictType dictType) {
        if (Constants.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictType))) {
            return error("新增字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.insertDictType(dictType));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
        modelMap.put("dict", dictTypeService.getById(dictId));
        return PREFIX + "/edit";
    }

    /**
     * 修改保存字典类型
     */
    @ResponseBody
    @PostMapping("/edit")
    public AjaxResult<Object> editSave(@Validated SysDictType dictType) {
        if (Constants.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictType))) {
            return error("修改字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.updateDictType(dictType));
    }

    /**
     * 清空缓存
     */
    @ResponseBody
    @GetMapping("/clearCache")
    public AjaxResult<Object> clearCache() {
        dictTypeService.clearCache();
        return success();
    }

    /**
     * 查询字典详细
     */
    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
        modelMap.put("dict", dictTypeService.getById(dictId));
        modelMap.put("dictList", dictTypeService.selectDictTypeAll());
        return "system/dict/data/data";
    }

    /**
     * 校验字典类型
     */
    @ResponseBody
    @PostMapping("/checkDictTypeUnique")
    public String checkDictTypeUnique(SysDictType dictType) {
        return dictTypeService.checkDictTypeUnique(dictType);
    }

    /**
     * 选择字典树
     */
    @GetMapping("/selectDictTree/{columnId}/{dictType}")
    public String selectDeptTree(@PathVariable("columnId") Long columnId, @PathVariable String dictType, ModelMap modelMap) {
        modelMap.put("columnId", columnId);
        modelMap.put("dict", dictTypeService.selectDictTypeByType(dictType));
        return PREFIX + "/tree";
    }

    /**
     * 加载字典列表树
     */
    @ResponseBody
    @GetMapping("/treeData")
    public List<Ztree> treeData() {
        return dictTypeService.selectDictTree(new DictTypeQuery());
    }

    /**
     * 删除字典类型（多个用逗号分隔）
     *
     * @param ids 数据id
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/remove")
    public AjaxResult<Object> remove(String ids) {
        List<Long> list = Arrays.asList(Convert.toLongArray(ids));
        return toAjax(dictTypeService.deleteDictTypeByIds(list));
    }

}
